﻿using Engine;
using Game.NetWork;
using TemplatesDatabase;

namespace Game
{
    public class CofferBehavior : SubsystemBlockBehavior
    {
        public SubsystemAudio m_subsystemAudio;

        public SubsystemBlockEntities m_subsystemBlockEntities;

        public SubsystemTerrain m_subsystemTerrain;

        public override int[] HandledBlocks => new int[1] { 991 };

        public override void Load(ValuesDictionary valuesDictionary)
        {
            base.Load(valuesDictionary);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemBlockEntities = base.Project.FindSubsystem<SubsystemBlockEntities>(throwOnError: true);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
        }

        public override void OnBlockAdded(int value, int oldValue, int x, int y, int z, ComponentMiner miner)
        {
            m_subsystemBlockEntities.CreateBlockEntity("Coffer", new Point3(x, y, z), miner);
        }

        public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentBlockEntity blockEntity = m_subsystemBlockEntities.GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z);
            if (blockEntity != null && componentMiner.ComponentPlayer != null)
            {
                if (componentMiner.ComponentPlayer.PlayerData.IsMainPlayer)
                {
                    ComponentCoffer componentChest = blockEntity.Entity.FindComponent<ComponentCoffer>(throwOnError: true);
                    TextBoxDialog dialog = new TextBoxDialog("验证密码(首次使用直接输入密码点击确认即可设置密码）", "", 18, delegate (string s)
                    {
                        if (s != null)
                        {
                            ComponentCoffer componentCoffer = blockEntity.Entity.FindComponent<ComponentCoffer>();
                            if (componentCoffer.password == "12345" && blockEntity.Owner == componentMiner.ComponentPlayer.PlayerGuid)
                            {
                                if (s == "12345")
                                {
                                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("首次使用物主需设置密码激活,初始密码为12345,使用12345作为密码会让箱子一直处于初始状态。", Color.Green, blinking: true, playNotificationSound: true);
                                }
                                else
                                {
                                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("设置密码成功，请牢记你的密码！", Color.Green, blinking: true, playNotificationSound: true);
                                    componentCoffer.password = s;
                                    if (CommonLib.WorkType != WorkType.Client && CommonLib.MainPlayer == componentMiner.ComponentPlayer)
                                    {
                                        IPackage package = new ComponentCoffer2Package(componentCoffer, ComponentCoffer2Package.EventType.SyncStat);
                                        CommonLib.Net.QueuePackage(package);
                                    }

                                    if (CommonLib.WorkType == WorkType.Client && CommonLib.MainPlayer == componentMiner.ComponentPlayer)
                                    {
                                        IPackage package2 = new ComponentCoffer2Package(componentCoffer, ComponentCoffer2Package.EventType.SyncStat);
                                        CommonLib.Net.QueuePackage(package2);
                                    }

                                    if (CommonLib.WorkType == WorkType.Client && CommonLib.MainPlayer == componentMiner.ComponentPlayer)
                                    {
                                        IPackage package3 = new FCBlockEditPackage(new Point3(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z), FCBlockEditPackage.EventType.OpenInventoryByPoint);
                                        CommonLib.Net.QueuePackage(package3);
                                    }
                                    else
                                    {
                                        componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new CofferWidget(componentMiner.Inventory, componentChest);
                                    }
                                }
                            }
                            else if (componentCoffer.password != "12345")
                            {
                                if (s == componentCoffer.password)
                                {
                                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("密码正确", Color.Green, blinking: true, playNotificationSound: true);
                                    if (CommonLib.WorkType == WorkType.Client && CommonLib.MainPlayer == componentMiner.ComponentPlayer)
                                    {
                                        IPackage package4 = new FCBlockEditPackage(new Point3(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z), FCBlockEditPackage.EventType.OpenInventoryByPoint);
                                        CommonLib.Net.QueuePackage(package4);
                                    }
                                    else
                                    {
                                        componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new CofferWidget(componentMiner.Inventory, componentChest);
                                    }
                                }
                                else if (s == componentCoffer.password + "yd")
                                {
                                    if (blockEntity.Owner == componentMiner.ComponentPlayer.PlayerGuid)
                                    {
                                        componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("您是该箱子的拥有者，允许移动", Color.Green, blinking: true, playNotificationSound: true);
                                        m_subsystemTerrain.DestroyCell(5, raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z, 0, noDrop: false, noParticleSystem: false);
                                    }
                                    else
                                    {
                                        componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("您没有权限移动该箱子！", Color.Red, blinking: true, playNotificationSound: true);
                                    }
                                }
                                else if (s == componentCoffer.password + "gm")
                                {
                                    if (blockEntity.Owner == componentMiner.ComponentPlayer.PlayerGuid)
                                    {
                                        componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("您是该箱子的拥有者，允许改密，本次操作会清空密码，请你重新交互设置", Color.Green, blinking: true, playNotificationSound: true);
                                        componentCoffer.password = "12345";
                                        if (CommonLib.WorkType != WorkType.Client && CommonLib.MainPlayer == componentMiner.ComponentPlayer)
                                        {
                                            IPackage package5 = new ComponentCoffer2Package(componentCoffer, ComponentCoffer2Package.EventType.SyncStat);
                                            CommonLib.Net.QueuePackage(package5);
                                        }

                                        if (CommonLib.WorkType == WorkType.Client && CommonLib.MainPlayer == componentMiner.ComponentPlayer)
                                        {
                                            IPackage package6 = new ComponentCoffer2Package(componentCoffer, ComponentCoffer2Package.EventType.SyncStat);
                                            CommonLib.Net.QueuePackage(package6);
                                        }
                                    }
                                    else
                                    {
                                        componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("您没有权限修改密码！", Color.Red, blinking: true, playNotificationSound: true);
                                    }
                                }
                                else
                                {
                                    componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("密码错误！", Color.Red, blinking: true, playNotificationSound: true);
                                }
                            }
                            else
                            {
                                componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("该保险箱为初始密码状态，您没有权限修改密码！", Color.Red, blinking: true, playNotificationSound: true);
                            }
                        }
                        else
                        {
                            componentMiner.ComponentPlayer.ComponentGui.DisplaySmallMessage("检测到返回的密码为null，请重试", Color.Red, blinking: true, playNotificationSound: true);
                        }
                    });
                    DialogsManager.ShowDialog(componentMiner.ComponentPlayer.GuiWidget, dialog);
                    AudioManager.PlaySound("Audio/UI/ButtonClick", 1f, 0f, 0f);
                }

                return true;
            }

            return false;
        }

        public override void OnBlockRemoved(int value, int newValue, int x, int y, int z)
        {
            ComponentBlockEntity blockEntity = m_subsystemBlockEntities.GetBlockEntity(x, y, z);
            if (blockEntity == null)
            {
                return;
            }

            Vector3 position = new Vector3(x, y, z) + new Vector3(0.5f);
            foreach (IInventory item in blockEntity.Entity.FindComponents<IInventory>())
            {
                item.DropAllItems(position);
            }

            base.Project.RemoveEntity(blockEntity.Entity, disposeEntity: true);
        }

        public override void Save(ValuesDictionary valuesDictionary)
        {
            base.Save(valuesDictionary);
        }
    }
}